<?php
class Training_FileController extends Zendvn_Controller_Action{
	
	public function init(){
		
	}
	
	public function  index9aAction(){
		
		$upload = new Zendvn_File_Upload();
		if($this->_request->isPost()){
			
			//$fileName = $upload->upload('upload',FILE_PATH . '/upload/abc/',array('rename'=>true));
			$fileName = $upload->upload('upload',FILE_PATH . '/upload/abc/');
			echo '<br>' . $fileName;
		}
		//$upload->upload()
		$this->_helper->viewRenderer('index2');
		
	}
	
	public function  index9Action(){
		
		//1. Khoi tao
		$upload = new Zend_File_Transfer_Adapter_Http();
		//2. Thiet lap thu muc upload
		$fileDir = FILE_PATH . '/upload/abc/';
		$upload->setDestination($fileDir,'upload');
		//3. Upload
		$upload->receive('upload');
		
		//param Thu muc upload
		//param File field
		//param options 
		//		1. Giu nguyen ten cua tap tin
		//		2. Doi ten
		//		3. Neu doi ten thi ten ?
	}
	
	public function  index8Action(){
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileDir = FILE_PATH . '/upload/abc/';
		$upload->setDestination($fileDir,'upload');
		
		if($this->_request->isPost()){
			//$validator = new Zend_Validate_File_Count($options);
			$files = $upload->getFileInfo();
			
			
			$upload->addValidator('Count',true,array('min'=>1,'max'=>3),$files);
			if(!$upload->isValid($files)){
				echo '<br> <h3>Error</h3>';
				$message = $upload->getMessages();
				/*echo '<pre>';
				print_r($message);
				echo '</pre>';*/
				echo '<br>' . current($message);
			}
			
			echo '<pre>';
			print_r($files);
			echo '</pre>';
		}
		
		//$this->_helper->viewRenderer('index2');
	}
	public function index7Action(){

		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileDir = FILE_PATH . '/upload/abc/';
		$upload->setDestination($fileDir,'upload');
		
		if($this->_request->isPost()){
			//$validator = new Zend_Validate_File_Exists()($options);
			$newFileName = time() . '.jpg';
			
			$upload->addFilter('Rename',array( 'target'=> $fileDir  . $newFileName),'upload');				
			$upload->receive('upload');
			
		}
		$this->_helper->viewRenderer('index2');
	}
	
	public function index6Action(){

		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileDir = FILE_PATH . '/upload/abc/';
		$upload->setDestination($fileDir,'upload');
		
		if($this->_request->isPost()){
			//$validator = new Zend_Validate_File_Exists()($options);
		
			
			$upload->addValidator('NotExists',true,$fileDir,'upload');
		
			if(!$upload->isValid('upload')){
				$message = $upload->getMessages();
				echo '<br>' . current($message);
			//	$filter = new Zend_Filter_File_Rename();
				$options = array(
								  //'source'=>$upload->getFileName('upload'),
								  'target'=> $fileDir  . 'a12345.jpg',
								  //'overwrite'=>'a12345.jpg'
								);
				//$this->addFilter('Rename',array('target'=> $dir_upload . '/' . $newFileName),$file_name);
				$upload->addFilter('Rename',array( 'target'=> $fileDir  . 'a12345.jpg'),'upload');
				
				$upload->receive('upload');
				echo '<pre>';
				print_r($upload);
				echo '</pre>';
			}else{
				echo '<br>'	. 'Cho phep upload';
				$upload->receive();
			}
			
			
			
		}
		$this->_helper->viewRenderer('index2');
	}
	
	
	public function index5Action(){

		$upload = new Zend_File_Transfer_Adapter_Http();
		
		if($this->_request->isPost()){
			//$validator = new Zend_Validate_File_MimeType($options);
		
			
			$options = array('maxheight'=>1000,'maxwidth'=>1000);
			//$upload->addValidator('Extension',true,'jpg,rar','upload');
			//$upload->addValidator('MimeType',true,array('image/jpeg'),'upload');
			echo '<pre>';
			print_r($upload);
			echo '</pre>';
			if(!$upload->isValid('upload')){
				$message = $upload->getMessages();
				echo '<br>' . current($message);
			}else{
				echo '<br>'	. 'Cho phep upload';
			}
			
			$upload->setDestination(FILE_PATH . '/upload/abc/','upload');
			
		}
		$this->_helper->viewRenderer('index2');
	}
	
	public function index4Action(){

		$upload = new Zend_File_Transfer_Adapter_Http();
		if($this->_request->isPost()){
			//$validator = new Zend_Validate_File_Extension($options);
			//$validator = new Zend_Validate_File_FilesSize($options);
			//$validator = new Zend_Validate_File_ImageSize($options);
			//$validator = new Zend_Validate_File_IsImage($options);
			
			$options = array('maxheight'=>1000,'maxwidth'=>1000);
			//$upload->addValidator('Extension',true,'jpg,rar','upload');
			$upload->addValidator('Extension',true,array('jpg'),'upload')
					->addValidator('FilesSize',true,array('min'=>'10kb','max'=>'2Mb'),'upload')
					->addValidator('IsImage',true,null,'upload');
					//->addValidator('ImageSize',true,$options,'upload');
			if(!$upload->isValid('upload')){
				$message = $upload->getMessages();
				echo '<br>' . current($message);
			}else{
				echo '<br>'	. 'Cho phep upload';
			}
			
			$upload->setDestination(FILE_PATH . '/upload/abc/','upload');
			
		}
		$this->_helper->viewRenderer('index2');
	}
	
	public function index3Action(){
		echo '<br>' . __METHOD__;
		$upload = new Zend_File_Transfer_Adapter_Http();
		if($this->_request->isPost()){
			$upload->setDestination(FILE_PATH . '/upload/abc/','upload');
			echo '<br>' . $upload->getFileName('upload',false);
			echo '<br>' . $upload->getFileSize('upload');
			$upload->receive('upload');
			echo '<br>' . $upload->getMimeType('upload');
			echo '<pre>';
			print_r($upload->getFileInfo());
			echo '</pre>';
			//echo '<br>' . $upload->getType();
		}
		$this->_helper->viewRenderer('index2');
	}
	
	
	public function index2Action(){
		echo '<br>' . __METHOD__;
		$upload = new Zend_File_Transfer_Adapter_Http();
		if($this->_request->isPost()){
			$upload->setDestination(FILE_PATH . '/upload/abc/','upload');
			$upload->setDestination(FILE_PATH . '/upload/bdf/','upload2');
			$info = $upload->getFileInfo();
			echo '<pre>';
			print_r($info);
			echo '</pre>';
			$upload->receive(array('upload'));
		}
		$this->_helper->viewRenderer('index');
	}
	
	public function indexAction(){
		echo '<br>' . __METHOD__;
		$upload = new Zend_File_Transfer_Adapter_Http();
		if($this->_request->isPost()){
			$upload->setDestination(FILE_PATH);
			$upload->receive();
		}
	}
	
	
}